#Appendix: Amnesty Provision Descriptives

load("Amnesty Provision Main Data.rdata")

#Figure A1#####
measures<-data[grep("fragmentation", colnames(data))]
measures<-measures[c(3,1,2)]
colnames(measures)<-c("Average Substitutes", "No Infighting", "Forge")
measures<-log(measures+1)

limitRange <- function(data, mapping, ...) { 
  ggplot(data = data, mapping = mapping, ...) + 
    geom_point(..., alpha=.3) + 
    geom_smooth(method = "lm", se = FALSE) +
    scale_y_continuous(limits = c(0, 1.5)) +
    scale_x_continuous(limits = c(0, 1.5))+
    geom_abline(slope=1)
  }

p<-ggpairs(measures, lower=list(continuous=limitRange),
           upper = list(continuous = wrap("cor", size = 5)))
p+theme_bw()
pdf("FigA1.pdf", width = 4, height = 4)
print(p)
dev.off()

#Table 1 ######
dat<-data[,c("Amn", "log_frag2", "bin", "Inc", "v2x_cspart", "v2x_polyarchy", "v2x_clphy", "v2x_rule")]

colnames(dat)<-c("Amnesty Provision",
                 "Average Substitutes",
                 "Any Substitutes",
                 "Incompatibility",
                 "Civil Society Strength",
                 "Electoral Democracy",
                 "Political Violence",
                 "Rule of Law")

tab<-stargazer(data.frame(dat), digits=3, 
               omit.summary.stat = c("p25", "p75"), nobs=T, median = T)
tab
write(tab, file="Table 1.tex")

#Figure A2 #####
dat<-na.omit(dat)
fake<-cor(dat)
fake<-ifelse(abs(fake)>.6,fake,NA)
corrplot(cor(dat), type = "upper", diag=F, method="square", col=viridis(200),  tl.col="black")
corrplot(fake, type = "upper", diag=F, method="square",   col=viridis(200), bg = "transparent", tl.col = "transparent", addCoef.col = "black",
         na.label = " ", add = T)
pdf("FigA2.pdf")
corrplot(cor(dat), type = "upper", diag=F, method="square", col=viridis(200),  tl.col="black")
print(corrplot(fake, type = "upper", diag=F, method="square",   col=viridis(200), bg = "transparent", tl.col = "transparent", addCoef.col = "black",
         na.label = " ", add = T))
dev.off()


#Table 2 #####
tab<-table(data[,c("bin", "Amn")])
#Get Fisher's Exact test 
print(fisher.test(tab))
#Output table
tab<-print(xtable(tab, digits=0))
tab
write(tab, file="Table 2.tex")

#Figure A4######
load("Amnesty Provision Main Data.Rdata")

#Bootstrap CI
n<-1000
set.seed(1015)
coef<-matrix(NA, ncol=1)
#Compare exemptions with non exemptions
data<-na.omit(data[data$Amn==1,c("CID","amn_with_ex", "bin", "Inc","v2x_cspart","v2x_polyarchy","v2x_clphy",
                                   "v2x_rule")])
pop<-unique(data$CID)

i<-1
while(length(na.omit(coef))<n){
  sample<-sample(pop, replace = T, size = length(pop))
  map<-sapply(sample, function(x) which(data[,"CID"]==x))
  boot.sample<-data[unlist(map),]
  model<-brglm(amn_with_ex~bin+as.factor(Inc)+
                 v2x_cspart+v2x_polyarchy+v2x_clphy+v2x_rule, data=boot.sample, family="binomial")
  if(is.na(model)) next
  if(model$converged==F) next
  coef[i]<-summary(margins(model))[1,2]
  #cat("\r", i, "of", n) 
  i<-i+1
  model<-NA
}

m<-brglm(amn_with_ex~bin+as.factor(Inc)+
           v2x_cspart+v2x_polyarchy+v2x_clphy+v2x_rule, data=data)

point<-summary(margins(m))[1,2]
plot.dat<-cbind.data.frame(point, t(sort(na.omit(coef), decreasing = F)[c(25,975)]),"Effect of Substitute Groups")
colnames(plot.dat)[2:4]<-c("lb", "ub", "level")

figA4<-ggplot(plot.dat, aes(y=point, x=level))+
  coord_flip()+
  geom_point(size=3, position=position_dodge(width=.5))+
  geom_linerange(aes(ymin=lb, ymax=ub), position=position_dodge(width=.5))+
  geom_hline(yintercept=0)+
  xlab("")+
  ylim(c(0,1))+
  ylab("Marginal Effect on Amnesty Exemptions")+
  theme_bw()
pdf("FigA4.pdf", width=6, height=2)
print(figA4)
dev.off()

rm(list=setdiff(ls(), c("cluster_bootstrap", "int_plot")))

